package com.asyf.consumer.controller;

import com.asyf.common.service.CommonService;
import com.asyf.consumer.feignservice.FeignDemoService;
import com.asyf.consumer.feignservice.FeignSingleThreadPoolClient;
import feign.Feign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
@RestController
@RequestMapping(value = "")
public class DemoController {
    final AtomicInteger atomicInteger = new AtomicInteger();

    @Autowired
    private FeignDemoService feignDemoService;

    @Autowired
    private FeignSingleThreadPoolClient feignSingleThreadPoolClient;

    @Autowired
    private CommonService commonService;

    //    @Autowired
    private Map<String, Feign> feignMap;


    @GetMapping(value = "/test")
    public String test() throws InterruptedException {
        atomicInteger.set(0);
/*        Runnable runnable = new Runnable() {

            public void run() {
                String test = feignDemoService.test();
                log.info(test + "----" + Thread.currentThread());
                atomicInteger.incrementAndGet();
            }
        };*/
/*        for (int i = 0; i < 1; i++) {
            new Thread(runnable).start();
        }*/
//        Thread.sleep(10000);
        String test = feignDemoService.test();
        log.info(test + "----" + Thread.currentThread());
        return test;
    }

    @GetMapping(value = "/testGet")
    public Object test2(String a) {
        return new Date();
    }


    /**
     * 测试独立线程池
     * 问题1：
     * #设置为feign.hystrix.enabled=true feign.read-timeout超时就不生效了（生效：hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000)
     * feign.hystrix.enabled=true
     *
     * @return
     * @throws InterruptedException
     */
    @GetMapping(value = "/test3")
    public String test3() throws InterruptedException {
        String test = feignSingleThreadPoolClient.test();
        log.info(test + "----" + Thread.currentThread());
        return test;
    }

}
